I'm beginner in learning NLP. I read about the CFG and I want to apply it in top-down parsing and bottom-up parsing. I started with top-down parsing. I want to draw the top-down parsing tree with nltk and python 36. I wrote the following code, but it does not work. what is the wrong? Is there anyone could help me enhance the code?
import nltk from nltk.tag import pos_tag from nltk.tokenize import word_tokenize from nltk.tree import * from nltk.draw import tree from nltk import Nonterminal, nonterminals, Production, CFG from nltk.parse import RecursiveDescentParser text = input('Please enter a sentence: ') words = text.split() sentence = pos_tag(words)
grammar1 = nltk.CFG.fromstring(""" S -> NP VP S -> VP VP -> V NP | V NP PP NP -> Det N | Det N PP PP -> P NP V -> "saw" | "ate" | "walked" | "book" | "prefer" | "sleeps" Det -> "a" | "an" | "the" | "my" | "that" N -> "man" | "dog" | "cat" | "telescope" | "park" | "flight" | "apple" P -> "in" | "on" | "by" | "with" """)
rd = nltk.RecursiveDescentParser(grammar1, "Input") result = rd.parse(sentence) result.draw() I entered this text for parsing "book that flight".
You must be logged in to post. Please login or register an account.